<template>
  <div
    style="height: 505px; overflow: hidden; background: white; border:1px solid white ; border-radius: 8px; margin-top: 10px;padding: 9px;">
    <el-button style="position: absolute; right: 87px; top:55px; z-index: 100;" type="primary" size="mini"
      @click=" dialogVisible2 = true " round>增加</el-button>
    <el-button style="position: absolute; right: 26px; top:55px; z-index: 100;" type="danger" size="mini"
      @click="delProcess()" :disabled='multipleSelection.length > 0 ? false :true' round>删除</el-button>
    <el-table stripe border ref="multipleTable" :data="tableData" size="medium " tooltip-effect="dark"
      style="width: 100%; height: 394px;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="工序项目编号" width="260">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column prop="gxname" label="工序项目名称" width="220">
      </el-table-column>
      <el-table-column prop="desc" label="工序项目描述" width="220">
      </el-table-column>
      <el-table-column prop="personNo" label="负责人编号" width="120">
      </el-table-column>
      <el-table-column label="变更" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="updateProcess(scope.row)" round="">变更</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="display: block; margin-top: 25px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
        :page-sizes="[1, 3, 7]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog title="工序变更" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="工序项目编号" :label-width="formLabelWidth" size="mini">
          <el-input v-model="form.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="工序项目名称" :label-width="formLabelWidth" size="mini">
          <el-input v-model="form.gxname" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="工序项目描述" :label-width="formLabelWidth">
          <el-input v-model="form.desc" style="height: 49px;"></el-input>
        </el-form-item>
        <el-form-item label="负责人编号" :label-width="formLabelWidth">
          <el-input v-model="form.personNo" style="height: 49px;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button style="width: 45px ; height: 30px;" @click="dialogVisible = false">取 消</el-button>
        <el-button style="width: 45px ; height: 30px;" type="primary" @click="save()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="增加新的工序" :visible.sync="dialogVisible2" width="40%" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="工序项目名称" :label-width="formLabelWidth" size="mini">
          <el-input v-model="form.gxname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="工序项目描述" :label-width="formLabelWidth">
          <el-input v-model="form.desc" style="height: 49px;"></el-input>
        </el-form-item>
        <el-form-item label="负责人编号" :label-width="formLabelWidth">
          <el-input v-model="form.personNo" style="height: 49px;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button style="width: 45px ; height: 30px;" @click="dialogVisible2 = false">取 消</el-button>
        <el-button style="width: 45px ; height: 30px;" type="primary" @click="newsave()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'repsel',
    data() {
      return {
        pageNum: 1,
        pageSize: 7,
        total: 0,
        booldisabled: true,
        tableData: [],
        gridData: [],
        form: {
          id: '',
          gxname: '',
          desc: '',
          personNo: ''
        },
        formLabelWidth: '120px',
        multipleSelection: [],
        dialogVisible: false,
        dialogVisible2: false
      }
    },
    methods: {
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
          this.multipleSelection = [];
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = [];
        for (var i = 0; i < val.length; i++) {
          this.multipleSelection.push(val[i].id)
        }
        console.log(this.multipleSelection);

      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.GetOnline(this.pageNum, this.pageSize);
      },
      handleCurrentChange(val) {
        this.pageNum = val;
        this.GetOnline(this.pageNum, this.pageSize);
      },
      GetOnline(pageNum, pageSize) {
        var data = new URLSearchParams();
        data.append("pageNum", pageNum);
        data.append("pageSize", pageSize);
        this.$http.post('production/product/getList  ', data).then(res => {
          this.pageNum = res.data.pageNum;
          this.pageSize = res.data.pageSize;
          this.total = res.data.total;
          this.tableData = res.data.rows;
        }).catch(err => {
          console.log(err);
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      updateProcess(data) {
        this.form.id = data.id;
        this.form.desc = data.desc;
        this.form.personNo = data.personNo;
        this.form.gxname = data.gxname;
        this.dialogVisible = true

      },
      save() {
        var data = this.form;
        this.$http.post('production/product/updateProcess', this.$qs.stringify(data)).then(res => {
          if (res.status == 200) {
            this.$message({
              message: '已变更库存',
              type: 'success'
            });
            
            this.form.id = '';
            this.form.desc = '';
            this.form.personNo = '';
            this.form.gxname = '';
            
            this.dialogVisible = false;
          }
        }).catch(err => {
          this.$message.error('请输入正确的负责人号');
        })
      },
      newsave() {
        var data = this.form;

        this.$http.post('production/product/insertProcess', this.$qs.stringify(data)).then(res => {
          if (res.status == 200) {
            this.$message({
              message: '已添加',
              type: 'success'
            });
            this.form.id = '';
            this.form.gxname = '';
            this.form.desc = '';
            this.form.personNo = '';
            this.dialogVisible2 = false;
            this.GetOnline(this.pageNum, this.pageSize);
          }
        }).catch(err => {
          this.$message.error('请输入正确的负责人号');
        })
      },
      delProcess() {
        if (this.multipleSelection.length > 0) {
          var val = this.multipleSelection;
          var data =  new URLSearchParams();
          data.append("id",val)
          this.$http.post('production/product/delProcess', data).then(res => {
            if (res.status == 200) {
              this.$message({
                message: '已删除',
                type: 'success'
              });
            }
            this.GetOnline(this.pageNum, this.pageSize);
          }).catch(err => {
            console.log(err);
          })
        }
      }
    },
    mounted() {
      this.GetOnline(this.pageNum, this.pageSize);
    }
  }
</script>

<style scoped="scoped">
  * {
    padding: 0px;
    margin: 0px;
  }

  .el-breadcrumb__inner {
    color: black;

  }

  .el-input__inner {

    height: 25px;

  }

  .el-input__prefix,
  .el-input__suffix {
    position: absolute;
    top: 7px;
    -webkit-transition: all .3s;
    height: 100%;
    color: #C0C4CC;
    text-align: center;
  }

  .el-table td {
    padding: 10px 0;
  }

  .el-input__inner {
    height: 28px;
  }
</style>
